package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Dept;
import com.aaa.entity.User;
import com.aaa.service.DeptService;
import com.aaa.vo.DeptVo;
import com.aaa.vo.Result;
import com.aaa.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptDao, Dept> implements DeptService {
    @Autowired
    private DeptDao deptDao;
    @Override
    public Result<List<Dept>> findAll() {
        QueryWrapper<Dept> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Dept> list = deptDao.selectList(wrapper);
        return new Result<List<Dept>>(2000,"查询所有部门",list);
    }
    //分页查询
    @Override
    public Result<IPage<Dept>> findByConditions(Integer currentPage, Integer pageSize, DeptVo deptVo) {
        IPage<Dept> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Dept> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(deptVo.getDeptName())){
            wrapper.like("dept_name",deptVo.getDeptName());
        }
        if(StringUtils.hasText(deptVo.getDeptLeader())){
            wrapper.like("dept_leader",deptVo.getDeptLeader());
        }
        if(StringUtils.hasText(deptVo.getDeptNumber())){
            wrapper.like("dept_number",deptVo.getDeptNumber());
        }
        if(StringUtils.hasText(deptVo.getLeaderPhone())){
            wrapper.eq("leader_phone",deptVo.getLeaderPhone());
        }
        if(Objects.nonNull(deptVo.getDeptId())){
            wrapper.eq("dept_id",deptVo.getDeptId());
        }
        if(Objects.nonNull(deptVo.getOrderNum())){
            wrapper.eq("order_num",deptVo.getOrderNum());
        }
        if(Objects.nonNull(deptVo.getRegNumber())){
            wrapper.eq("reg_number",deptVo.getRegNumber());
        }
        if(Objects.nonNull(deptVo.getStatus())){
            wrapper.eq("status",deptVo.getStatus());
        }
        if(Objects.nonNull(deptVo.getDateRange())&&deptVo.getDateRange().length==2){
            wrapper.between("create_time",deptVo.getDateRange()[0],deptVo.getDateRange()[1]);
        }
        IPage<Dept> page1 = deptDao.selectPage(page, wrapper);
        return new Result<IPage<Dept>>(2000,"查询用户成功",page1);
    }

    @Override
    public boolean delById(Long id) {
        int i = deptDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean saveAndUpdate(Dept dept) {
        int i=-1;
        if (dept.getCreateTime()==null){
            dept.setCreateTime(new Date());
            dept.setUpdateTime(new Date());
        }else {
            dept.setUpdateTime(new Date());
        }
        if(dept.getDeptId()==null){
            i=deptDao.insert(dept);
            System.out.println("====="+dept.getStatus());
        }else {
            i=deptDao.updateById(dept);
        }
        if (i>0){
            return true;
        }
        return false;
    }
    //模块接口
    @Override
    public List<Dept> findDeptAll() {
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("status", 0);
        return deptDao.selectList(qw);
    }

    @Override
    public Dept getByDeptId(Long deptId) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_id", deptId);

        return deptDao.selectOne(wrapper);
    }

    @Override
    public String updNum(Long deptId, Integer regNumber) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setRegNumber(regNumber);
        int i = deptDao.updateById(dept);
        return i>0?"成功":"失败";
    }
}
